aDataSet = [];
deleteImg = '<img id="delete" src="../images/icons/cross.png" />';
updateImg = '<img id="update" src="../images/icons/edit.png" />';
$(document).ready(function(){
	
	$.ajax({
		type : 'Get',
		url : '/qRH-WS/services/question/getAll',
		async: false,
		dataType : 'json',
		crossDomain : true,
		success : function(result, textStatus, jqXHR) {
			jQuery.each(result, function() {
				aDataSet.push(eval([this.idQuestion, this.contenuQuestion, this.categorieDto.libelleCategorie, updateImg, deleteImg]));		 
			});   
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(xhr.responseText);
		}
	});
	
	$('#tableContainer').html( '<table class="display" id="dataTableCandidat"></table>' );
	
	$('#dataTableCandidat').dataTable( {
		"aaData": aDataSet,
		"aoColumns": [
            { "sTitle": "Id" },
			{ "sTitle": "Contenu" },
			{ "sTitle": "Categorie" },
			{ "sTitle": "Modifier" },
			{ "sTitle": "Supprimer" }
			]
	} );
	
	$('#dataTableCandidat tbody tr').each(
		function() {
			
	        var nTds = $('td', this);
	        var idQuestion = $(nTds[0]).text();
	        
	        $('td', this).slice(-1).click(
        		function() {
		        	var answer = confirm("Voulez-vous vraiment supprimer cette question ?");
					if(answer) {
						$.ajax({
							type : 'Delete',
							url : '/qRH-WS/services/question/delete',
							async: false,
							data : idQuestion,
							dataType : 'json',
							success : location.reload()
						});
		        	};
		        }
	        );
	        
	        $('td', this).slice(-2,-1).click(
        		function() {
        			window.location.href = "creerOuModifierQuestion.html?idQuestion="+idQuestion;
		        }
	        );
	    }
	);
});